<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#	
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
# functions.inc.php:
#
#		Provides utility functions to other files	
##############################################################################################

// Config has all the values
require_once("config.inc.php");

# Global variables
	$global_database_link = NULL;
# End of Global variables

# MySQL Functions

	// Connects to MySQL database and selects the database
	function connect() {
		
		global 	$MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_HOST, $MYSQL_DATABASE, $global_database_link;
		
		if ( $global_database_link == NULL ) {
			mysql_pconnect ( $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD );
			$global_database_link = mysql_select_db ( $MYSQL_DATABASE );
		}
	}
	
	// Connects to database and queries it
	function query ( $mysql_query_string ) {
	
		global $DEBUG;
		
		connect();
				
		if ( $DEBUG ) echo $mysql_query_string;
		
		$result = mysql_query ( $mysql_query_string ) or 
							die(mysql_error()." : ".$mysql_query_string);
		
		return $result;
	}

# End of MySQL Functions

// Report all errors in DEBUG mode
if ( $DEBUG ) error_reporting(E_ALL);

// Returns the path like http://yourdomain.com/t/add (if called from url_compressor.php
function get_path() {

	global $MOD_REWRITE;

	$path = "http://".$_SERVER['HTTP_HOST'];
	$p = explode("/",$_SERVER['PHP_SELF']);
	for($i=0; $i<(sizeof($p)-1); $i++) {
		$path .= $p[$i]."/";
	}

	// Append query string if mod rewrite is not available
	if(!$MOD_REWRITE)
		$path.="?q=";

	return $path;
}

// Similar to get_path() 
// But returns the path like http://yourdomain.com/t/
function get_outer_path() {
	
	global $MOD_REWRITE;
	
	$path = "http://".$_SERVER['HTTP_HOST'];
	$p = explode("/",$_SERVER['PHP_SELF']);
	
	for($i=0; $i<(sizeof($p)-2); $i++) {
		$path .= $p[$i]."/";
	}
	
	// Append query string if mod rewrite is not available
	if(!$MOD_REWRITE)
		$path.="?q=";
		
	return $path;
}

// Sanitise GETs and POSTs
function prevent_mysql_injection() {

	foreach($_POST as $post => $value) {
		$_POST[$post] = mysql_escape_string($value);
	}
	
	foreach($_GET as $get => $value) {
		$_POST[$get] = mysql_escape_string($value);
	}
}

// Exits
function finish() {
	// Thought of mysql_close here but it is returning error
	// PHP documentation says it is not necessary anyways
	
	// Go for an early
	exit;
}

// Renders 404 page. You can customize it if you want to
function page_not_found() {

	// Tell browser 
	header("HTTP/1.0 404 Not Found");
	
	// Tell user
	echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
	<html>
		<head>
			<title>404 Not Found</title>
		</head>
		<body>
			<h1>Not Found</h1>
			<p>The requested URL '.$_SERVER['REQUEST_URI'].' was not found on this server.</p>
			<hr>
			<table style="width:100%" cellspacing=0 cellpadding=0>
				<tr>
					<td>
						<address>'.$_SERVER['SERVER_SIGNATURE'].'</address>
					</td>
					<td style="text-align:right">
						<address>
							Generated by <a href="http://code.google.com/p/compressurl">URL Compressor</a>
						</address>
					</td>
				</tr>
			</table>
		</body>
	</html>
';
	finish();
}
?>